package com.martin.dms.dataservices.hib;

import java.util.List;

import org.hibernate.Session;

import com.martin.dms.dataservices.ClientDataService;
import com.martin.dms.model.Client;
import com.martin.dms.model.hib.ClientHib;

/**
 * Hibernate implementation of the Client data service.
 * 
 * @author mtsvetkov
 */
public class ClientDataServiceHib implements ClientDataService {

    private static final String CLIENT_GET_ALL_QUERY = "Client.getAll";

    private Session session;

    /**
     * Constructor that expects a hibernate session.
     * 
     * @param session - the current hibernate session.
     */
    public ClientDataServiceHib(Session session) {
        this.session = session;
    }

    /**
     * {@inheritDoc}
     */
    public Client create(Client dmsObject) {
        ClientHib client = new ClientHib();
        client.setAddress(dmsObject.getAddress());
        client.setEmail(dmsObject.getEmail());
        client.setFirstName(dmsObject.getFirstName());
        client.setLastName(dmsObject.getLastName());
        client.setUsername(dmsObject.getUsername());
        client.setPassword(dmsObject.getPassword());
        client.setPhoneNumber(dmsObject.getPhoneNumber());
        session.save(client);
        return client;
    }

    /**
     * {@inheritDoc}
     */
    public void delete(long id) {
        ClientHib client = get(id);
        session.delete(client);
    }

    /**
     * {@inheritDoc}
     */
    public ClientHib get(long id) {
        return (ClientHib) session.get(ClientHib.class, Long.valueOf(id));
    }

    /**
     * {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    public List<Client> getAll() {
        return session.getNamedQuery(CLIENT_GET_ALL_QUERY).list();
    }

    /**
     * {@inheritDoc}
     */
    public void updateAddress(long clientId, String address) {
        ClientHib client = get(clientId);
        client.setAddress(address);
    }

    /**
     * {@inheritDoc}
     */
    public void updateEmail(long clientId, String email) {
        ClientHib client = get(clientId);
        client.setEmail(email);
    }

    /**
     * {@inheritDoc}
     */
    public void updateNames(long clientId, String firstName, String lastName) {
        ClientHib client = get(clientId);
        client.setFirstName(firstName);
        client.setLastName(lastName);
    }

    /**
     * {@inheritDoc}
     */
    public void updatePassword(long clientId, String password) {
        ClientHib client = get(clientId);
        client.setPassword(password);
    }

    /**
     * {@inheritDoc}
     */
    public void updatePhoneNumber(long clientId, String phoneNumber) {
        ClientHib client = get(clientId);
        client.setPhoneNumber(phoneNumber);
    }

    /**
     * {@inheritDoc}
     */
    public void updateUsername(long clientId, String username) {
        ClientHib client = get(clientId);
        client.setUsername(username);
    }

}
